
window.gaId = window.gaId || "";
window.gtag = window.gtag || function (){dataLayer.push(arguments);};

if (gaId) {
	document.write('<script async src=\"https://www.googletagmanager.com/gtag/js?id=' + gaId + '\"><\/script>');
	add_ga(gaId);
}else{
	window.gtag = function (){};
}


function add_ga(gaId) {
		window.dataLayer = window.dataLayer || [];
		
		function gtag() {
			dataLayer.push(arguments);
		}
		gtag("js", new Date());
		
		gtag("config", gaId);
	}
	
	function show_indexAd_one() {
		if (!indexAdSlot_one && !indexAdDiv_one) return;
		
		googletag.cmd.push(function () {
			//广告展示
			googletag.pubads().addEventListener("impressionViewable",
			function (event) {
				// console.log('impressionViewable', event.slot.getSlotElementId());
				gtag("event", "impressionViewable", {
					event_category: "googleAds",
					
					event_label: "viewable  " + event.slot.getSlotElementId(),
				});
			});
			
			// 发出广告请求
			googletag.pubads().addEventListener("slotRequested",
			function (event) {
				// console.log('slotRequested', event.slot.getSlotElementId());
				gtag("event", "slotRequested", {
					event_category: "googleAds",
					
					event_label: "requested:" + event.slot.getSlotElementId(),
				});
			});

		// 开始加载广告位
		googletag.pubads().addEventListener("slotOnload",
			function (event) {
				// console.log('SlotOnLoadEvent', event.slot.getSlotElementId());
				gtag("event", "SlotOnLoadEvent", {
					event_category: "googleAds",

					event_label: "slotonload:" + event.slot.getSlotElementId(),
				});
			});

		//注入素材
		googletag.pubads().addEventListener("slotRenderEnded",
			function (event) {
				// console.log('SlotRenderEndedEvent', event.slot.getSlotElementId());
				gtag("event", "SlotRenderEndedEvent", {
					event_category: "googleAds",

					event_label: "renderEnded:" + event.slot.getSlotElementId(),
				});
			});

		//广告响应
		googletag.pubads().addEventListener("slotResponseReceived",
			function (event) {
				// console.log('SlotResponseReceivedEvent', event.slot.getSlotElementId());
				gtag("event", "SlotResponseReceivedEvent", {
					event_category: "googleAds",

					event_label: "respone:" + event.slot.getSlotElementId(),
				});
			});

		//屏幕发生改变
		googletag.pubads().addEventListener("slotVisibilityChanged",
			function (event) {
				// console.log('SlotVisibilityChangedEvent', event.slot.getSlotElementId());
				gtag("event", "SlotVisibilityChangedEvent", {
					event_category: "googleAds",

					event_label: "ViewChange:" + event.slot.getSlotElementId(),
				});
			});
		googletag.defineSlot(indexAdSlot_one, [ [300, 250]], indexAdDiv_one).addService(googletag.pubads());
		googletag.pubads().enableSingleRequest();
		googletag.enableServices();
	});
}
function show_indexAd_two() {
	if (!indexAdSlot_two && !indexAdDiv_two) return;
	
	document.addEventListener("DOMContentLoaded",
		function () {
			let showAd2 = true;
			const observer = new IntersectionObserver((entries) => {
				entries.forEach((entry) => {
					if (showAd2 && entry.isIntersecting) {
						showAd2 = false;
						
						googletag.cmd.push(function () {
							googletag.defineSlot(indexAdSlot_two, [ [300, 250]], indexAdDiv_two).addService(googletag.pubads());
							googletag.pubads().enableSingleRequest();
							googletag.enableServices();
						});
						googletag.cmd.push(function () {
							googletag.display(indexAdDiv_two);
						});
					} else {
					
					}
				});
			},
				{
					root: null,
					rootMargin: "0px",
					threshold: 0.5,
				});

			const targetElement = document.getElementById(indexAdDiv_two).parentElement;

			observer.observe(targetElement);
		});
}
function show_anchor() {
	if (!anchorSlotId) return;
	console.log("show_anchor");
	var anchorSlot;
	var staticSlot;
	googletag.cmd.push(function () {
	
		if (document.documentElement.clientWidth <= 500) {
			anchorSlot = googletag.defineOutOfPageSlot(anchorSlotId, googletag.enums.OutOfPageFormat.TOP_ANCHOR);
		} else {
			anchorSlot = googletag.defineOutOfPageSlot(anchorSlotId, googletag.enums.OutOfPageFormat.BOTTOM_ANCHOR);
		}
		if (anchorSlot) {
			anchorSlot.setTargeting("test", "anchor").addService(googletag.pubads());
		}

		staticSlot = googletag.defineSlot(anchorSlotId, [[300, 100], [728, 90],], "static-ad-1").addService(googletag.pubads());
		googletag.pubads().enableSingleRequest();
		googletag.enableServices();
	});
	googletag.cmd.push(function () {
		googletag.display(anchorSlot);
	});
}
function show_Intertials() {
	if (!intertialsSlotId) return;
	
	googletag.cmd.push(function () {
		var slot = googletag.defineOutOfPageSlot(intertialsSlotId, googletag.enums.OutOfPageFormat.INTERSTITIAL);
		
		if (slot) slot.addService(googletag.pubads());
		googletag.enableServices();
		
		googletag.display(slot);
	});
}

(function () {
	window.googletag = window.googletag || {
		cmd: [],
	};

	window.indexAdSlot_one = window.indexAdSlot_one || "";
	window.indexAdDiv_one = window.indexAdDiv_one || "";
	window.indexAdSlot_two = window.indexAdSlot_two || "";
	window.indexAdDiv_two = window.indexAdDiv_two || "";
	window.anchorSlotId = window.anchorSlotId || "";
	window.intertialsSlotId = window.intertialsSlotId || "";
	show_indexAd_one();
	show_indexAd_two();
	show_Intertials();
	show_anchor();
})();
